package com.sjtu.rest;

import com.sjtu.config.AdminHandlerFilter;
import com.sjtu.config.Operator;
import com.sjtu.feign.JobRunHistoryFeign;
import com.sjtu.feign.UserAccountFeign;
import com.sjtu.web.BaseRest;
import com.sjtu.web.Result;
import com.sjtu.web.form.JobRerunForm;
import com.sjtu.web.search.JobRunHistorySearch;
import com.sjtu.web.vo.UserAccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务运行历史
 * nerine
 * 1/29/2020
 */
@RequestMapping("/admin/history")
@RestController
public class JobRunHistoryRest extends BaseRest {
    @Autowired
    private UserAccountFeign userAccountFeign;
    @Autowired
    private JobRunHistoryFeign jobRunHistoryFeign;

    @PostMapping("/list")
    public Result<?> list(@RequestHeader(AdminHandlerFilter.OPERATOR_ID) String operatorId,
            @RequestBody JobRunHistorySearch search) {
        UserAccountVo userAccountVo = userAccountFeign.findByUserId(operatorId).getData();
        if(userAccountVo.isNotAdmin()) {
            search.setSystemId(userAccountVo.getClientInfo().getSystemId());
        }
        return jobRunHistoryFeign.list(search);
    }

    @Operator
    @PostMapping("/rerun")
    public Result<?> rerun(@RequestHeader(AdminHandlerFilter.OPERATOR_ID) String operatorId,
            @RequestBody JobRerunForm form) {
        return jobRunHistoryFeign.rerun(form);
    }
}
